Imports AUtility

Partial Public Class HeadToHead
    Inherits System.Web.UI.Page

    Dim selectStr As String

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Not IsPostBack Then
            FillIgrac1()
        End If

    End Sub

    Private Sub FillIgrac1()
        selectStr = String.Format("SELECT DISTINCT ImePrezime, IgracId FROM vIgrac ORDER BY ImePrezime")

        ddlIgrac1.DataTextField = "ImePrezime"
        ddlIgrac1.DataValueField = "IgracId"
        ddlIgrac1.DataSource = DataAccess.GetDataTable(GlobalTenisLiga.ConnectionString, selectStr)
        ddlIgrac1.DataBind()

    End Sub
    Private Sub FillIgrac2(ByVal igracId As Integer)
        selectStr = String.Format("SELECT DISTINCT ImePrezime, IgracId FROM vSusreti WHERE SusretId IN ( SELECT SusretId FROM vSusreti WHERE SezonaId <> 7 AND IgracId = {0} ) AND IgracId <> {0}", igracId)

        ddlIgrac2.DataTextField = "ImePrezime"
        ddlIgrac2.DataValueField = "IgracId"
        ddlIgrac2.DataSource = DataAccess.AddBlankRow(DataAccess.GetDataTable(GlobalTenisLiga.ConnectionString, selectStr))
        ddlIgrac2.DataBind()

    End Sub

    Protected Sub ddlIgrac1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ddlIgrac1.SelectedIndexChanged
        FillIgrac2(Convert.ToInt32(ddlIgrac1.SelectedValue))
        lblIgrac2.Visible = True
        lblSusreti.Visible = False

        lblScore.Visible = False
        lblScoreValue.Visible = False
    End Sub

    Protected Sub ddlIgrac2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ddlIgrac2.SelectedIndexChanged
        If ddlIgrac2.SelectedItem.Text <> "" Then
            Dim igrac2 As Integer = Convert.ToInt32(ddlIgrac2.SelectedValue)
            HeadToHead(Convert.ToInt32(ddlIgrac1.SelectedValue), igrac2)
            lblSusreti.Visible = True
        End If
    End Sub

    Private Sub HeadToHead(ByVal igrac1 As Integer, ByVal igrac2 As Integer)

        lblScore.Visible = True
        lblScoreValue.Visible = True
        'lblSusretiHead.Text = "Head To Head"
        selectStr = String.Format(" SELECT LigaNaziv, ImePrezime, Set1, Set2, Set3, convert(varchar,Datum,104) AS Datum2, Lokacija FROM vSusreti WHERE SusretId IN ( SELECT SusretId FROM vSusreti WHERE SezonaId <> 7 AND IgracId = {0} AND SusretId IN ( SELECT SusretId FROM vSusreti WHERE SezonaId <> 7 AND IgracId = {1} ) ) ORDER BY Datum DESC", igrac1, igrac2)

        lblSusreti.Text = ""
        Dim resultsDT As DataTable = DataAccess.GetDataTable(GlobalTenisLiga.ConnectionString, selectStr)

        For i As Integer = 0 To resultsDT.Rows.Count - 1 Step 2
            lblSusreti.Text = lblSusreti.Text + "<br>" + Result(resultsDT, i)
        Next

        Dim odigrane As Integer = Convert.ToInt32(resultsDT.Rows.Count / 2)
        selectStr = String.Format(" SELECT COUNT(*) FROM vSusreti WHERE SusretId IN ( SELECT SusretId FROM vSusreti WHERE SezonaId <> 7 AND IgracId = {0} AND SusretId IN ( SELECT SusretId FROM vSusreti WHERE SezonaId <> 7 AND IgracId = {1} ) ) AND Pobjednik = 1 AND IgracId = {0}", igrac1, igrac2)
        Dim pobjede As Integer = DataAccess.GetInt(GlobalTenisLiga.ConnectionString, selectStr)

        lblScoreValue.Text = String.Format("{0} - {1}", pobjede, odigrane - pobjede)

    End Sub

    Private Function Result(ByVal resultDT As DataTable, ByVal rowIndex As Integer) As String
        Dim resultTable As String = ""
        If (resultDT.Columns.Contains("LigaNaziv")) Then
            resultTable = String.Format("<table><tr><td colspan=4 align=center  bgcolor=#F0F0F0>{0}</td></tr><tr><td colspan=4 align=center>{1} - {2}</td></tr><tr bgcolor=#E0E0E0><td>{3}</td><td>{4}</td><td>{5}</td><td>{6}</td></tr><tr><td>{7}</td><td>{8}</td><td>{9}</td><td>{10}</td></tr></table>", resultDT.Rows(rowIndex)("LigaNaziv"), resultDT.Rows(rowIndex)("Datum2"), resultDT.Rows(rowIndex)("Lokacija"), resultDT.Rows(rowIndex)("ImePrezime"), resultDT.Rows(rowIndex)("Set1"), resultDT.Rows(rowIndex)("Set2"), resultDT.Rows(rowIndex)("Set3"), resultDT.Rows(rowIndex + 1)("ImePrezime"), resultDT.Rows(rowIndex + 1)("Set1"), resultDT.Rows(rowIndex + 1)("Set2"), resultDT.Rows(rowIndex + 1)("Set3"))
        Else
            resultTable = String.Format("<table><tr><td colspan=4 align=center>{0} - {1}</td></tr><tr bgcolor=#E0E0E0><td>{2}</td><td>{3}</td><td>{4}</td><td>{5}</td></tr><tr><td>{6}</td><td>{7}</td><td>{8}</td><td>{9}</td></tr></table>", resultDT.Rows(rowIndex)("Datum2"), resultDT.Rows(rowIndex)("Lokacija"), resultDT.Rows(rowIndex)("ImePrezime"), resultDT.Rows(rowIndex)("Set1"), resultDT.Rows(rowIndex)("Set2"), resultDT.Rows(rowIndex)("Set3"), resultDT.Rows(rowIndex + 1)("ImePrezime"), resultDT.Rows(rowIndex + 1)("Set1"), resultDT.Rows(rowIndex + 1)("Set2"), resultDT.Rows(rowIndex + 1)("Set3"))
        End If


        Return resultTable
    End Function
End Class